﻿using OAuth2Authorizer;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Web;

namespace OAuth2AuthorizerDemo.Models
{
    public class OAuthProvider
    {
        public string Name { get; set; }
        public string RegisterUrl { get; set; }
        public string RedirectUrl { get; set; }
        public string AppKey { get; set; }
        public string AppSecret { get; set; }
        public string AccessCode { get; set; }
        public string StateString { get; set; }
        public NameValueCollection AuthParameters { get; set; }
        public NameValueCollection TokenParameters { get; set; }

        protected IAuthorizer _authorizer = null;

        public OAuthProvider() { }
        public OAuthProvider(string name, string appKey, string appSecret, string stateString)
        {
            Name = name;
            AppKey = appKey;
            AppSecret = appSecret;
            StateString = stateString;
        }

        public virtual void Initialize(IAuthorizer authorizer, string redirectUrl)
        {
            _authorizer = authorizer;
            RedirectUrl = redirectUrl;
            RegisterUrl = _authorizer.GetAuthorizeUrl(AppKey, RedirectUrl, StateString, AuthParameters);
        }

        public virtual void GetAccessToken(string code)
        {
            var token = _authorizer.GetAccessToken(AppKey, AppSecret, RedirectUrl, code, TokenParameters);
            AccessCode = token.AccessToken;
        }
    }
}